// 主界面
;define(function(require, exports, module) {
    'use strict';
    var util = require("util");
    var base = require("base");
    var router = require("../hash/pageRouter");
    var css_this = require("./recieveNoticeDetail.css");
    var dialog = require("dialog");
    var validate = require("validate");
    var databind = require("databind");
    var pageIndex = 1,
        pageSize = 10,
        totalPageCount = 1,
        totalRecordCount = 0; //分页初始化
    var imgSrc;
    var recieveNoticeId;
    var noticeState;
    var left;
    var financeId;
    var init = function(page){
    	recieveNoticeId = page.params.recieveNoticeId||base.getUrlParam("recieveNoticeId");
    	noticeState = page.params.noticeState||base.getUrlParam("noticeState");
    	left = base.getUrlParam("left");
    	if(left==null){
			left=1;
		}
    	showNewTaskDetail(recieveNoticeId);
		initEvent();
		router.init();
    };
	var initEvent = function(){
		//若未登录，则返回登陆页面
		if(base.getData("sid")==null||base.getData("sid")==""||base.getData("sid")==undefined){
			top.location.href="../login/login.html";
		}
		//点击详情
		$(".page-content").on("click",".taskDetail",function(){
			router.route("financing/recieveNoticeDetail.html?recieveNoticeId="+recieveNoticeId+"&noticeState="+noticeState+"&left="+left,true);
		});
		
		//点击
		$(".newTaskList").unbind("click").click(function(){
			top.location.href = "../financing/recieveNoticeList.html?noticeState="+noticeState+"&left="+left;
		});
		
		//查看理货任务
		$(".page-content").on("click",".undoneCargoRecord",function(){
			router.route("financing/undoneCargoRecord.html?recieveNoticeId="+recieveNoticeId,true);
		});
		
		//接受理货任务
		$(".page-content-main").on("click",".acceptTasks",function(){
			dialog.showConfirmDialog("理货通知","确认要接受此理货通知么？",function(){
				base.fnAjax({
            action: "confirmRecieveNotice.json",
            oData: {
            	recieveNoticeId: recieveNoticeId
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
					 base.alert(1,"确认理货任务成功");
					 router.route("financing/newTaskDetail.html?recieveNoticeId="+recieveNoticeId,true);
                } else {
                     base.alert(2,"信息获取失败");
                }
            }
        });
			});
		});
		
		//完成理货任务
		$(".page-content-main").on("click",".finishedTasks",function(){
			dialog.showConfirmDialog("完成理货任务","确定要完成此理货任务吗",function(){
				base.fnAjax({
            action: "finishedRecieveNotice.json",
            oData: {
            	recieveNoticeId: recieveNoticeId
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
					 base.alert(1,"理货任务完成");
					 location.href = "../financing/recieveNoticeList.html?noticeState=2&left=2";
                } else {
                     base.alert(2,"信息获取失败");
                }
            }
        });
			});
		});
		//生成理货单
		$(".page-content-main").on("click",".tallyListGenerate",function(){
			router.route("financing/tallyListGenerate.html?recieveNoticeId="+recieveNoticeId+"&left="+left+"&noticeState="+noticeState,true);
			});
    };
    
     var showNewTaskDetail = function(recieveNoticeId){
    	base.fnAjax({
            action: "inquireRecieveNoticeDetail.json",
            oData: {
            	recieveNoticeId: recieveNoticeId
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	financeId = data.datas[0].financeId;
                	inquireFinanceApplicationDetail(financeId);
                	var html ;
                	var newTaskDetailTpl = $("#newTaskDetailTpl").html();
					if(newTaskDetailTpl!=undefined){
                		html = juicer(newTaskDetailTpl, data);
                	}
					$(".page-content-main-content").html(html);
					
					databind.fillForm(".dialogContentNewTask",{recieveNoticeId:recieveNoticeId});
					var noticeState = data.datas[0].noticeState;
					if(noticeState==1){
						 $(".acceptTasks").css("display", "inline-block");
						 $(".tallyListGenerate").css("display", "none");
						 $(".bar").css("display", "none");
						  $(".undoneCargoRecord").css("display", "none");
						 
					}else if(noticeState==2){
						 $(".acceptTasks").css("display", "none");
						 $(".tallyListGenerate").css("display", "inline-block");
						 $(".bar").css("display", "inline-block");
						   $(".undoneCargoRecord").css("display", "inline-block");
						   $(".finishedTasks").css("display", "inline-block");
					}
					
					
                } else {
                     base.alert(2,"信息获取失败");
                }
            }
        });
    	
    }
    var inquireFinanceApplicationDetail = function(financeId){
    	base.fnAjax({
            action: "inquireFinanceApplicationDetail.json",
            oData: {
            	financeId: financeId
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
					var html ;
                	var newTaskDetailTpl = $("#financeDetailTpl").html();
					if(newTaskDetailTpl!=undefined){
                		html = juicer(newTaskDetailTpl, data);
                	}
					$(".financeDetail").html(html);
                } else {
                     base.alert(2,"信息获取失败");
                }
            }
        });
    }
     
    return {
        init:init
    };
});